package com.hsy.crm.web.web;

import com.google.gson.JsonObject;
import com.hsy.crm.web.config.CrmAuthenticationToken;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Response;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.PlatformCustomer;
import com.hsy.crm.web.domain.po.login.LoginPo;
import com.hsy.crm.web.domain.vo.login.LoginVo;
import com.hsy.crm.web.service.base.DataInitService;
import com.hsy.crm.web.service.impl.PlatCustomerService;
import com.hsy.crm.web.service.redis.RedisService;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.DictUtils;
import com.hsy.crm.web.utils.JsonUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.List;

@RestController
public class IndexAction extends BaseAction{

    @Autowired
    PlatCustomerService platCustomerService;

    @Autowired
    DataInitService dataInitService;

    @Autowired
    RedisService redisService;

    @RequestMapping("/login")
    public String login(LoginVo loginVo) {
        CrmAuthenticationToken token = new CrmAuthenticationToken(loginVo.toUser());
        Subject subject = SecurityUtils.getSubject();
        subject.getSession().setTimeout(43200000);
        try {
            subject.login(token);
            return "<script>parent.loged(0)</script>";
        }catch (Exception e){
            return "<script>parent.loged("+App.BussError.LOGIN_ERROR.getCode()+")</script>";
        }
    }

    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Response.body(Unified.newInstance());
    }

    private static JsonObject BUSS_ERROR_JSON = null;

    @RequestMapping("/bussError")
    public String bussError(){
        if(BUSS_ERROR_JSON==null){
            BUSS_ERROR_JSON = new JsonObject();
            App.BussError[] bussErrors = App.BussError.values();
            for( App.BussError bussError : bussErrors ){
                BUSS_ERROR_JSON.addProperty("code"+bussError.getCode(),bussError.getMessage());
            }
        }

        LoginPo login = (LoginPo) SecurityUtils.getSubject().getPrincipal();
        JsonObject json = new JsonObject();
        json.addProperty("serverTime",String.valueOf(DateUtils.millis()));

        json.addProperty("loginName",login.getName());
        json.addProperty("loginCompanyName",login.getCompanyName());

        json.addProperty("loginUserId",login.getUserId());
        json.addProperty("customerId",login.getCustomerId());
        json.addProperty("loginCompanyId",String.valueOf(login.getCompanyId()));
        json.addProperty("loginCompanyLogicalCode",login.getCompanyLogicalCode());
        json.add("bussError",BUSS_ERROR_JSON);

        json.add("dicts",JsonUtils.toJsonArray(JsonUtils.toJson(DictUtils.getCustomerDicts(login.getCustomerId()))));

        return json.toString();
    }

    @PostConstruct
    public void init(){
        List<PlatformCustomer> customers = platCustomerService.selectAll();
        dataInitService.initProductIds(customers);
        dataInitService.initVendorIds(customers);
        dataInitService.initCustomerIds(customers);
        dataInitService.initCodeIndex(customers);
        dataInitService.initDict();
        dataInitService.initMenus();
        dataInitService.initStoreProductDetailIndex(customers);

        //cache all customer products
        //redisService.init(customers);
    }
}
